<?php
	$jqueryPlugin = new Plugin('jQueryLibrary');
	$jqueryPlugin->addPlugin( 'scripts', 'jQueryLibrary' );
	$jqueryPlugin->Author('Ramblingwood, USA. jQuery by John Resig and the jQuery team at jquery.com');
	$jqueryPlugin->Description( 'This includes the jQuery library from either the Google AJAX API library or from a local copy.' );
	$jqueryPlugin->version( '0.5' );
	$jqueryPlugin->addInterface('jQueryInterface');
	$jqueryPlugin->addInstall('jQueryInstall');
	$jqueryPlugin->addUninstall('jQueryUninstall');
	$jqueryPlugin->addSidebarBlock('Where is my jQuery hosted?', 'jQuerySidebar');
	function jQueryInstall () {
		global $jqueryPlugin;
		$jqueryPlugin->addOption('location', 'local');
	}
	function jQueryUninstall () {
		global $jqueryPlugin;
		$jqueryPlugin->deleteOption('location');
	}
	function jQuerySidebar () {
		global $jqueryPlugin;
		echo 'jQuery is hosted at: '.$jqueryPlugin->getOption('location');
	}
	function jQueryInterface () {
		global $jqueryPlugin;
		if($_POST['save']) {
			if($jqueryPlugin->updateOption('location', $_POST['location']))
				echo $jqueryPlugin->message('success', 'Location Saved', 'Woohoo!');
			else
				echo $jqueryPlugin->message('error', 'Hmmm...Something didn\'t work...', 'Please try again.');
		}
		$loc = $jqueryPlugin->getOption('location');
		if($loc == 'google') {
			$options = "<option selected='selected' value='google'>Hosted on Google</option><option value='local'>Hosted on my server</option>";
		}
		else {
			$options = "<option value='google'>Hosted on Google</option><option selected='selected' value='local'>Hosted on my server</option>";
		}
		include($jqueryPlugin->SERVER_ROOT.'plugins/jQueryLibrary/functions.php');
		$plugins = glob($jqueryPlugin->SERVER_ROOT.'plugins/jQueryLibrary/addons/*', GLOB_ONLYDIR);
		if($plugins !== array()) {
			foreach ($plugins as $key => $value) {
				$SpluginDir = explode('.php', $value);
				$SpluginDir = $SpluginDir[0].'/';
				$pluginDir = $root.'plugins/jQueryLibrary/addons/'.end(explode('/',$value)).'/';
				require($SpluginDir.'jQueryPlugin.php');
				$count = count($pluginInfo)+1;
				$pluginInfo .= "<li>$name</li>";
			}
		}
		echo <<<EOT
	<form action="{$jqueryPlugin->VISUAL_ROOT}admin.php?page=plugins&interface=jquerylibrary" method="post">
		<fieldset>
			<h2>jQuery Library Settings</h2>
			<div class="form-row">
				<label for="location">Location of jQuery<span class="small gray">You can use the copy of jQuery hosted on Google's servers which would 
				decrease load time and reduce bandwidth. However, you have more control over a local copy.</span></label>
				<span>
					<select name="location">
						{$options}
					</select>
				</span>
			</div>
			<div class="form-row form-row-last">
				<label for="save">&nbsp;</label>
				<span>
					<input name='save' value='Save' type="submit" />
				</span>
			</div>
		</fieldset>
	</form>
	<form>
		<fieldset>
			<h2>jQuery Library Addons</h2>
			<p>These are found in the plugins/jQueryLibrary/addons folder. Delete a folder if you don't want the addon anymore.</p>
			<ul>
{$pluginInfo}
			</ul>
		</fielset>
	</form>
EOT;
	}
	function windowsToUnixPath ($str) {
		$str = preg_replace('/[a-zA-Z]:\\\/', '\\', $str);
		$str = str_replace("\\",'/', $str);
		return $str;
	}
	
	
	function jQueryLibrary() {
		global $jqueryPlugin;
		$root = $jqueryPlugin->SERVER_ROOT;
		include($root.'plugins/jQueryLibrary/functions.php');
		$plugins = glob($root.'plugins/jQueryLibrary/addons/*', GLOB_ONLYDIR);
		if($plugins !== array()) {
			foreach ($plugins as $key => $value) {
				$SpluginDir = explode('.php', $value);
				$SpluginDir = $SpluginDir[0].'/';
				$pluginDir = $root.'plugins/jQueryLibrary/addons/'.end(explode('/',$value)).'/';
				require($SpluginDir.'jQueryPlugin.php');
				$count = count($pluginInfo)+1;
				$pluginInfo[$count]['name'] = $name;
				$pluginInfo[$count]['depends'] = $depends;
				$pluginInfo[$count]['css'] = $css;
				$pluginInfo[$count]['scripts'] = $scripts;
				$pluginInfo[$count]['onload'] = $onload;
				
				$activePlugins[] = $name;
			}
			foreach ($pluginInfo as $key => $value) {
				if($value['depends'] == false || $value['depends'] == "" || $value['depends'] == 'none') {
					$finalPluginList[] = $value;
				}
				else {
					$dependsList[] = $value;
				}
			}
			/*var_dump ($finalPluginList);
			foreach ($dependsList as $key => $value) {
				$key = search_in_array($value['depends'], $finalPluginList);
				var_dump( $key );
			}*/
			foreach($finalPluginList as $key => $value) {
				$totalCss .= $value['css'];
				$totalScripts .= $value['scripts'];
				$totalOnload .= $value['onload'];
			}
		}
		if($jqueryPlugin->getOption('location') == 'google') {
			$url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js';
		}
		else {
			$url = $root.'plugins/jQueryLibrary/jquery-1.3.1.min.js';
		}
		echo $totalCss;
		echo "<script type=\"text/javascript\" src=\"{$url}\"></script>\n";
		echo $totalScripts;
		if($totalOnload !== "") {
			echo <<<EOT
	<script type="text/javascript">
	$(document).ready(function() {
	{$totalOnload}
	});
	</script>

EOT;
		}
	}
?>